<%@page trimDirectiveWhitespaces="true"%>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.*" %>

<%

	//get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	// check session
		if (htp_session == null || htp_session.getAttribute("sUser") == null) {
		    // Forward the control to login.jsp if authentication fails or session expires
		    request.getRequestDispatcher("/login.jsp").forward(request,
		        response);
		}
		if(!"admin".equals(htp_session.getAttribute("sUser"))){%>
			<script>alert("This user dont have PERMISSION to access this zone.");</script>
			<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=system_select.jsp"> 	
	<%}
	
	//get parameter
	String index = request.getParameter("index");
	
	System.out.println("@ delete employee ");////////////try//////////
	System.out.println("delete : "+index);////////////try//////////
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	String databaseProps = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
		databaseProps = props.getProperty("database");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	
	// delete employee from database
	try {
		ResultSet rs = connect.createStatement().executeQuery("SELECT * FROM `account` "
					+" WHERE `index`='"+ index +"'" );
		
		if(rs.next()) {
			out.println("fail");
		}
		else {
			// delete employee
			String sql_emp = "DELETE FROM `employee` "
					+" WHERE `index`='"+ index +"'";
			
			connect.createStatement().executeUpdate(sql_emp);
			
			/* Log file */			
			String log = "admin delete employee";
			Log.writeAdminFile(log);
			Log.writeAdminFile(sql_emp);
			
			// delete people
			String sql_people = "DELETE  FROM `people` "
					+" WHERE `index`='"+ index +"'";
			
			connect.createStatement().executeUpdate(sql_people);
			
			/* Log file */			
			String log_people = "admin delete employee";
			Log.writeAdminFile(log_people);
			Log.writeAdminFile(sql_people);
			
			// delete fullname
			String sql_fullname = "DELETE  FROM `full_name` "
					+" WHERE `index`='"+ index +"'";
			
			connect.createStatement().executeUpdate(sql_fullname);
			
			/* Log file */			
			String log_fullname = "admin delete employee";
			Log.writeAdminFile(log_fullname);
			Log.writeAdminFile(sql_fullname);
			
			out.println("success");
			
			System.out.println("delete success");////////////try//////////
		}
			
	} catch (SQLException e) {
		out.println("fail DB");
		
		e.printStackTrace();
	}
	
	connect.close();

%>
